package com.panopset.gui;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;

import javax.swing.JFrame;

import com.panopset.Numbers;
import com.panopset.gui.LookAndFeel.Dimensions;
import com.panopset.swing.App;

/**
 * GUI Utilities.
 *
 * @author Karl Dinwiddie
 */
public final class UtilGUI {

    /**
     * @param prompt
     *            Prompt.
     * @return String from user.
     */
    public static String getStringFromUser(final String prompt) {
        TTextField tf = new TTextField(40);
        TPanel cp = new TPanel(new BorderLayout());
        cp.add(BorderLayout.CENTER, tf);
        new TypicalDialog.Builder(prompt).centerPanel(cp).size(
                new Dimension(Numbers.Integers.DEFAULT_DIALOG_WIDTH.getValue(),
                        120))
                .construct().getFrame().setVisible(true);
        return tf.getText();
    }

    /**
     * @return Standard centered frame.
     */
    public static JFrame createStandardFrame() {
        JFrame f = new JFrame();
        f.setSize(Dimensions.WINDOW.getValue());
        f.setLocation(
            com.panopset.Util.centerInScreen(Dimensions.WINDOW.getValue()));
        Image image = App.getIcon();
        if (image != null) {
            f.setIconImage(image);
        }
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        return f;
    }

    /**
     * Update a component. Calls invalidate on the component, then invalidate
     * on its parent, if the parent exists.
     *
     * @param comp Component.
     */
    public static void updateComponent(final Component comp) {
        comp.invalidate();
        Component p = comp.getParent();
        if (p != null) {
            p.validate();
        }
    }
    /**
     * Prevent instantiation.
     */
    private UtilGUI() { }
}
